<?php
class MemberController extends AdminBaseController {
	/**
	 * 自动执行
	 */
	public function _initialize() {
		parent::_initialize(); 
		$this->db = model('user');
	}

	public function orderadd(){

		if(IS_POST){
			$order_num = intval($_POST['order_num']);
			if($order_num<1)  showmessage('请填写数量');
			$min_num = intval($_POST['min_num']);
			$max_num = intval($_POST['max_num']);
			// 待随机的机器人
			$list = M("user")->field("id,username,is_robot")->where("is_robot")->select();
			// p($list);

			// die;

			$today = model('WalletPrice')->where(array('start'=>date('Y-m-d')))->find();
			$min_price = $today['min_price']*100;
			$max_price = $today['max_price']*100;


			// 次数
			for ($i=0; $i < $order_num; $i++) { 
				$rand_key = array_rand($list);
		        $user = $list[$rand_key];
		        // p($user);
		        $buy_num = mt_rand($min_num,$max_num);
		        $buy_price = mt_rand($min_price,$max_price)/100;

		        // 美元价格
		        $amount = bcmul($buy_num,$buy_price,2);
				// 本次交易的rmb价格
		        $CNY = bcmul($amount,C("site_cny_usd"),2);
		        // 本次交易的手续费
		        $_fee = bcmul($buy_num,(C("site_trate_fee")/100),2);

	            $data = array();
	            $data['order_sn'] = get_order_code("D");
	            // $data['user_id']买家id
	            $data['user_id'] = $user['id'];
	            $data['num'] = $buy_num;
	            $data['cny'] = $CNY;
	            $data['fee'] = $_fee;
	            $data['price'] = $buy_price;
	            $data['amount'] = $amount;
	            $data['type'] = 2;
	            $data['is_robot'] = 1;
	            // 要调取真实数据 
	            $order_count = (int)M("user_trade")->where(array(
	            	"user_id|sell_id"=>array("eq",$user['id']),
	            	"order_status"=>3
	            ))->count();

	            $data['order_num'] = $order_count;
	            // $data['order_appr'] = getUserAppr($user['id']);
	            $data['dateline']  = NOW_TIME;
	            $data['end_time'] = NOW_TIME+C("site_hour")*3600;//交易超时时间 
	            $rut2 = M("user_trade")->add($data);
	            echo M()->getlastsql()."<br>";
	            // die;
	            // if($rut2!==false){
	            // 	session("send_code_time",null);
	            //     showmessage('操作成功！',"",1);
	            // }else{
	            //     showmessage('操作失败');
	            // }

			}


	            if($rut2!==false){
	            	session("send_code_time",null);
	                showmessage('操作成功！',"",1);
	            }else{
	                showmessage('操作失败');
	            }



		}else{
			include $this->admin_tpl('order_add');
		}
	}



	/**
	 * 会员列表
	 */
	public function lists(){
		if(IS_POST){
			$sqlmap = array();
			//$field = 'id,username,true_name,group_id,mobile_phone,email,pay_points,user_money,exp,reg_time,user_type,compayname,old_rebate_num,rebate_num';
			$field = 'id,username,true_name,group_id,mobile_phone,email,user_money,freeze_money,exp,reg_time,user_type,is_renzheng,invite_id,invite_count,invite_all_count,is_ban';
			//$field = 'id,username,true_name,group_id,mobile_phone,email,pay_points,user_money,exp,reg_time,user_type,compayname,(select count(*) from ybk_user_rebate where status=0 and uid=u.id) as user_rebate,(select count(*) from ybk_user_rebate where uid=u.id) as user_old_rebate';
			//排序
			$_order=isset($_POST['order']) ? ($_POST['order']) : NULL;
			$_sort=isset($_POST['sort']) ? ($_POST['sort']) : NULL;
			if($_order && $_sort){
				$order[$_sort] = $_order;
			}else{
				$order['id'] = 'DESC';
			}
			//筛选
			// if(isset($_GET['group_id']) && $_GET['group_id']){//分类
			// 	$sqlmap['group_id']= $_GET['group_id'];
			// }
			if(isset($_GET['user_type']) && $_GET['user_type']){//分类
				$sqlmap['user_type']= $_GET['user_type'];
				if($sqlmap['user_type']=="-1") $sqlmap['user_type'] = 0;
			}

			if(isset($_GET['is_ban']) && $_GET['is_ban']){//分类
				$sqlmap['is_ban']= $_GET['is_ban'];
				$is_ban= $_GET['is_ban'];
				if($sqlmap['is_ban']=="-1") $sqlmap['is_ban'] = 0;
			}
			if(isset($_GET['is_renzheng']) && $_GET['is_renzheng']){//分类
				$sqlmap['is_renzheng']= $_GET['is_renzheng'];
				$is_renzheng= $_GET['is_renzheng'];
				if($sqlmap['is_renzheng']=="-1") $sqlmap['is_renzheng'] = 0;
			}


			if($_GET["start"]!="" or $_GET["end"]!=""){
					$start = strtotime($_GET['start'].' 00:00:00');
					$end = strtotime($_GET['end'].' 23:59:59');
					$sqlmap['reg_time'] =array('between',$start.','.$end);
			}

			$search_type = I("search_type",1);
			if(isset($_GET['keyword']) && $_GET['keyword']){//分类
				$keyword = $_GET['keyword'];
				

				if($search_type==1){
					$sqlmap['username|mobile_phone'] = array("LIKE", "%".$keyword."%");
				}
				if($search_type==2){
					$uumap['username|mobile_phone'] = array("LIKE", "%".$keyword."%");
					$uu = M("user")->where($umap)->find();
					$sqlmap['invite_id'] = $uu['id'];
				}elseif($search_type==3){
					$uumap['username|mobile_phone'] = array("LIKE", "%".$keyword."%");
					$uu = M("user")->where($umap)->find();
					$sqlmap['_string'] = 'FIND_IN_SET('.$uu['id'].',invite_str)';
				}else{
					$sqlmap['username|mobile_phone'] = array("LIKE", "%".$keyword."%");
				}

			} 

			// if(isset($_GET['td_keyword']) && $_GET['keyword']){//分类
			// 	$keyword = $_GET['keyword'];
			// 	$sqlmap['username|mobile_phone'] = array("LIKE", "%".$keyword."%");
			// }			
			//分页
			$pagenum=isset($_GET['page']) ? intval($_GET['page']) : 1;
			$rowsnum=isset($_GET['rows']) && (int)($_GET['rows']) != 0 ? intval($_GET['rows']) : PAGE_SIZE;
			//计算总数 
			$data['total'] = $this->db->where($sqlmap)->count();
			// $data['sql'] = M()->getlastsql();	
			// $data['group'] = M('UserGroup')->field('id,name')->order('sort ASC, id ASC')->select();
			//print_r($data["group"]);
			// $data['user_type'] = array(array("id"=>1,"name"=>"个人"),array("id"=>2,"name"=>"商家"));
			$user_type = C("USER_TYPE");
			foreach ($user_type as $key => $value) {
				$arr = array();
				$arr['id'] = $key==0?"-1":$key;
				$arr['name'] = $value;
				if($_GET['user_type']==$arr['id']) $arr['selected'] = true;
				$data['user_type'][]=$arr;
			}

			$data['search_type'] = array(
				array(
					'id'=>1,
					'name'=>"会员"
				),
				array(
					'id'=>2,
					'name'=>"直推"
				),
				array(
					'id'=>3,
					'name'=>"团队"
				),				
			);

			foreach ($data['search_type'] as $kk => $vv) {
				if($vv['id']==$search_type) $data['search_type'][$kk]['selected'] = true;
			}

			$data['ban_type'] = array(
				array(
					'id'=>"-1",
					'name'=>"正常"
				),
				array(
					'id'=>1,
					'name'=>"冻结"
				)
			);
			foreach ($data['ban_type'] as $kkk => $vvv) {
				if($vvv['id']==$is_ban) $data['ban_type'][$kkk]['selected'] = true;
			}
			$data['renzheng_type'] = array(
				array(
					'id'=>"-1",
					'name'=>"未认证"
				),
				array(
					'id'=>1,
					'name'=>"认证"
				)
			);
			foreach ($data['renzheng_type'] as $kkk => $vvv) {
				if($vvv['id']==$is_renzheng) $data['renzheng_type'][$kkk]['selected'] = true;
			}

			// print_r($data["user_type"]);
			$data['rows']=model('user u')->field($field)->where($sqlmap)->limit(($pagenum-1)*$rowsnum.','.$rowsnum)->order($order)->select();

			foreach ($data['rows'] as $key => $value) {
				$invite = $this->getuser($value['invite_id']);
				$data['rows'][$key]['invite_name'] = $invite['username'];
				unset($invite);
			}

			
			$data['ids'] = $this->db->where($sqlmap)->getField("id",true);
			$cmap = array();
			$cmap['status']=1;
			$cmap['user_id'] = array("in",$data['ids']);
			$data['miner_count']=M("miner")->field("type,count(id) as count,sum(min_rate) as rates ")->where($cmap)->group("type")->order("type asc")->select();

			$data["money"]=$this->db->where($sqlmap)->sum("user_money");
			$data['title']="统计概览-用户数量:".$data["total"]."&nbsp;&nbsp;累计账户余额:".($data["money"]==""?"0.00":$data["money"])."元, ";
			$rate = 0;
			foreach ($data["miner_count"] as $key => $value) {
				$miner = getMiner($value['type']);
				$str .=$miner['name'].":".$value['count']."个,";
				$rate = $rate+$value['rates'];
			}


			$data['title'] .="总算力为:".$rate.",";
			$data['title'] .=$str;
			if (!$data['group']) $data['group']=array();
			if (!$data['rows']) $data['rows']=array();
			echo json_encode($data);
		}else{
			include $this->admin_tpl('member_list');
		}
	}
	public function getuser($uid){
		$tmp = M("user")->find($uid);
		return $tmp;
	}
	/**
	 * 会员列表
	 */
	public function user_renzheng(){
		if(IS_POST){
			$sqlmap = array();
			$field = 'ur.*,u.id as uid,u.username,u.mobile_phone,u.user_type';
			//排序
			$_order=isset($_POST['order']) ? ($_POST['order']) : NULL;
			$_sort=isset($_POST['sort']) ? ($_POST['sort']) : NULL;
			if($_order && $_sort){
				$order[$_sort] = $_order;
			}else{
				$order['ur.id'] = 'DESC';
			}
			//筛选
			// if(isset($_GET['group_id']) && $_GET['group_id']){//分类
			// 	$sqlmap['group_id']= $_GET['group_id'];
			// }
			if(isset($_GET['user_type']) && $_GET['user_type']){//分类
				$sqlmap['ur.status']= $_GET['user_type'];
				if($sqlmap['ur.status']=="-1") $sqlmap['ur.status'] = 0;
			}
			if(isset($_GET['keyword']) && $_GET['keyword']){//分类
				$keyword = $_GET['keyword'];
				$sqlmap['username|mobile_phone'] = array("LIKE", "%".$keyword."%");
			}
			// $sqlmap['user_renzheng'] = array("gt",0);
			//分页
			$pagenum=isset($_GET['page']) ? intval($_GET['page']) : 1;
			$rowsnum=isset($_GET['rows']) && (int)($_GET['rows']) != 0 ? intval($_GET['rows']) : PAGE_SIZE;
			//计算总数 
			$data['total'] = M("user_renzheng ur")
			->join("__USER__ u on u.id=ur.user_id")
			->where($sqlmap)->count();	
			// echo M()->getlastsql();exit;
			$data['sqlmap'] = $sqlmap;
			$data['user_type'] = array(array("id"=>"-1","name"=>"未审核"),array("id"=>1,"name"=>"通过"),array("id"=>2,"name"=>"驳回"));
// p($data["user_type"]);
			$i=0;
			foreach($data['user_type'] as $key=>$val){
				if($_GET['user_type']===$key ){
					$data['user_type'][$key]["selected"]=true;					
				}
				$i+=1;
			}
			// p($data["user_type"]);
			// die;
			$data['rows']=M("user_renzheng ur")
			->join("__USER__ u on u.id=ur.user_id")->field($field)->where($sqlmap)->limit(($pagenum-1)*$rowsnum.','.$rowsnum)->order($order)->select();
			// echo M()->getlastsql();
			//print_r($data["rows"]);
			// $data["money"]=$this->db->where($sqlmap)->sum("user_money");
			// $data["user_old_rebate"]=M("user")->where($sqlmap)->sum("old_rebate_num");
			// $data["user_rebate"]=M("user")->where($sqlmap)->sum("rebate_num");
			// $data["user_old_rebate"]+=$data["user_rebate"];
			// $data['title']="统计概览-用户数量：".$data["total"]."&nbsp;&nbsp;累计账户余额：".($data["money"]==""?"0.00":$data["money"])."元"."&nbsp;&nbsp;累计消费可用赠送权：".($data["user_rebate"]==""?"0":$data["user_rebate"])."个"."&nbsp;&nbsp;累计消费赠送权：".($data["user_old_rebate"]==""?"0":$data["user_old_rebate"])."个";
			if (!$data['group']) $data['group']=array();
			if (!$data['rows']) $data['rows']=array();
			echo json_encode($data);
		}else{
			include $this->admin_tpl('member_user_renzheng_list');
		}
	}

	/**
	 * 认证处理
	 * @return [type] [description]
	 */
	public function setrenzheng(){

	    $id = $_GET['user_id'];
	    $status = $_GET['at'];
	    $msg = $_GET['msg'];
	    $info = M("user_renzheng")->find($id);
	    if($info['status']!=0) showmessage('审核失败！');
	    $user = M("user")->find($info['user_id']);
	    // 驳回
	    if($status==2){
			$save = array();
			$save['id'] = $info['id']; 
			$save['status'] = 2;
			$save["deal_user"]=ADMIN_ID;
			$save["deal_info"]=I("msg");
			$save['deal_time'] = NOW_TIME;
			$rut = M("user_renzheng")->save($save);
	    	if($rut){
	    		showmessage('操作成功！');
	    	}else{
	    		showmessage('操作失败！001');
	    	}
	    }
	    if($status==1){
			M()->startTrans();
	    	// 赠送矿机
	    	$order = array();
	    	$order = makeMinerOrderData(0);

	    	// p($order);
			$order['user_id'] = $info['user_id'];
			$order['buy_type'] = 1;//类型为赠送
			$order['order_sn'] = build_order_no();
			// 认证状态
			$save = array();
			$save['id'] = $info['id']; 
			$save['status'] = 1;
			$save["deal_user"]=ADMIN_ID;
			$save["deal_info"]=$msg;
			$save['deal_time'] = NOW_TIME;
			$rut = M("user_renzheng")->save($save);
			// 会员认证状态
			$rut1 = M("user")->where("id=".$user['id'])->save(array(
					"is_renzheng" =>1,
					"user_type" =>1,
					"true_name" =>$info['truename'],
					"idcard" =>$info['idcard'],
				));
			// echo M()->getlastsql();
			// 插入矿机
			$oid = M("miner")->add($order);
			// echo M()->getlastsql();
			$res3 = $res5 = $res6  = true;
	    	// 给上级发放奖励 
	    	if($user['invite_id']>0){

	    		updateInviteAllminingCount($user['invite_str'].$user['id'].",");
				// 更新上级开采率  遍历 联盟开采率包括自己 所以这样$user['invite_str'].$user['id'].","
				$res5 = updateInviteAllRate($user['invite_str'].$user['id'].",",1);
				//检测上级是否达到升级条件 遍历 刚通过认证肯定达不到升级奖励 所以不用检测自己
				$res6 = checkUserUp($user['invite_str']);
	    	}
	    	// var_dump($rut);
	    	// var_dump($rut1);
	    	// var_dump($oid);
	    	// var_dump($res3);
	    	// var_dump($res5);
	    	// var_dump($res6);
	    	// die;
	    	if($rut!==false && $rut1!==false && $oid!==false && $res3!==false && $res5!==false && $res6!==false){
	    		M()->commit();
	    		showmessage('操作成功！');
	    	}else{
	    		M()->rollback();
	    		showmessage('操作失败！002');
	    	}
	    }



	}



	public function dologin(){
		$url="/index.php?m=user&c=api&a=adminlogin&admintype=1&username=".$_GET["username"];
		header("Location:".$url);
	}
	/**
	 * 更新会员
	 */
	public function update(){
		// p($_POST);
		// die;
		$id=$_POST['id'];
		$dialog = TRUE;
		$_POST['birthday']=  strtotime($_POST['birthday']);
		if($id){
			if($this->db->create()){
				$this->db->save();
			}else{
				showmessage($this->db->getError());
			}  
		}else{
			 $_POST['reg_time']=  time();
			if($this->db->create()){
				$id=$this->db->add();
				// 更新联盟人数
				updateInviteAllCount($_POST['invite_str']);
			}else{
			   showmessage($this->db->getError());
			} 
		}
		if($id){
			showmessage('提交成功',U('Member/lists'), 1);
		}else{
			showmessage('提交成功',U('Member/add'), 1); 
		}
	}
	/**
	 * 添加会员
	 */
	public function add(){
		$validform = TRUE;
		$dialog = TRUE;
		// $agency=M("agency")->select();
		// $region = model('region')->where(array('parent_id' => 1))->select();
		// $city = model('region')->where(array('parent_id' => $info['province']))->select();
  //       $district = model('region')->where(array('parent_id' => $info['city']))->select();
		if(IS_POST){
			$_POST['valid'] = random(10);
			$_POST['mobile_phone']=trim($_POST['mobile_phone']);
			if(trim($_POST["password"])==""){
				$_POST['password'] = md5($_POST['valid'].substr(trim($_POST['mobile_phone']),-6));
				$_POST['pinpass'] = md5(substr(trim($_POST['mobile_phone']),-6));
			}else{
				$_POST['password'] = md5($_POST['valid'].$_POST['password']);
				$_POST['pinpass'] = md5($_POST['password']);
			}

			$_POST['invite_code'] = strtoupper(shorturl($_POST['username'])[0]);

			// if($_POST['is_renzheng']==1){
			// $_POST['user_type']=1;
			// }
			$_POST['user_type']=1;
			// 后台添加的会员 加个标记
			$_POST['source'] = 1;
			//推荐人 不填写推荐人或推荐人不存在报错
			//有推荐人的情况下 不可修改 不进if
			$yname = $_POST['yname'];
			// if(intval($_POST['invite_id'])==0){
				//有推荐人 
				if(!empty($yname)){
					$ymap['username|mobile_phone'] = $yname;
					$y_info = M("user")->field("id,username,invite_str")->where($ymap)->find();
					if($y_info){
						$_POST["invite_id"] = $y_info['id'];
						if($y_info['invite_str']!=""){
						   $_POST['invite_str'] = $y_info['invite_str'].$y_info['id'].",";
						}else{
						   $_POST['invite_str'] = ",".$y_info['id'].",";
						}
					}
					// else{
					// 	showmessage('推荐人填写不正确！');
					// 	die;
					// }
					unset($ymap);
					unset($_POST['yname']);
				}
				// else{
				// 	showmessage('请填写推荐人');
				// 	die;
				// }
			// }
			self::update();
		}else{
			$group = model('UserGroup')->where(array('status'=> 1))->order('sort ASC')->select();
			include $this->admin_tpl('member_add');
		}
	} 
	/**
	 * 编辑会员
	 */
	public function edit(){
		$validform = TRUE;
		$dialog = TRUE;
		$id = I('id', 0, 'intval');
		if(IS_POST){

			self::update();
		}else{
			$validform = '';
			$dialog = '';
			$info = $this->db->getById($id);
			// $agency=M("agency")->select();
			$opt="edit";
			// $region = model('region')->where(array('parent_id' => 1))->select();
			// $city = model('region')->where(array('parent_id' => $info['province']))->select();
			// $district = model('region')->where(array('parent_id' => $info['city']))->select();
			// $group = model('UserGroup')->where(array('status'=> 1))->order('sort ASC')->select();
			if($info['invite_id']>0){
				$yinfo = M("user")->field("id,username")->find($info['invite_id']);
			}
			include $this->admin_tpl('member_add');
		}
	} 
	/**
	 * 删除会员
	 */
	public function delete(){
		$id = (array) $_GET['id'];
		if(empty($id)) showmessage('参数错误');
		$sqlmap = $oauth_map = array();
		$sqlmap['id'] = $oauth_map['user_id'] = array("IN", $id);
		$this->db->where($sqlmap)->delete();
		model('user_oauth')->where($oauth_map)->delete();
		showmessage('数据删除成功', U('lists'), 1);
	}
	/**
	 * 重设密码
	 */
	public function repassword(){
		$id=$_GET['id'];
		if(!empty($id)){
			unset($where);
			$where['id']=$id;
			$info=$this->db->where($where)->find();
			$str=substr(trim($info['mobile_phone']),-6);
			$data['valid'] = random(10);
			$data['password'] = md5($data['valid'].$str);
			$this->db->where($where)->save($data);
			//邮件通知
			$mail=$info['email'];
			if($mail){
				//读取模板内容
				//$msg_info = M('MsgTemplate')->find(3);
				$title	= $msg_info['title'];
				$content  = $msg_info['content'];				
				//替换模板内容
				//$subject = str_replace(array('{$user_name}','{$password}','{$site_name}'),array($info['username'],'888888',C('site_name')),$content);
			//	$body	= str_replace(array('{$user_name}','{$password}','{$site_name}'),array($info['username'],'888888',C('site_name')),$content);
			//	SendMail($mail,$subject,$body);
			}
			showmessage('会员密码已设为'.$str.'手机号码后六位', U('Member/lists'), 1); 
		}else{
			showmessage('非法操作，请联系管理员！'); 
		}
	}
/**
	 * 重设二级密码
	 */
	public function repinpass(){
		$id=$_GET['id'];
		if(!empty($id)){
			unset($where);
			$where['id']=$id;
			$info=$this->db->where($where)->find();
			$str=substr(trim($info['mobile_phone']),-6);
			$data['pinpass'] = md5($str);
			$this->db->where($where)->save($data);
			//邮件通知
			$info=$this->db->where($where)->find();
			$mail=$info['email'];
			if($mail){
				//读取模板内容
				//$msg_info = M('MsgTemplate')->find(3);
				$title	= $msg_info['title'];
				$content  = $msg_info['content'];				
				//替换模板内容
				//$subject = str_replace(array('{$user_name}','{$password}','{$site_name}'),array($info['username'],'888888',C('site_name')),$content);
				//$body	= str_replace(array('{$user_name}','{$password}','{$site_name}'),array($info['username'],'888888',C('site_name')),$content);
				//SendMail($mail,$subject,$body);
			}
			showmessage('会员二级密码已设为'.$str.'手机号码后六位', U('Member/lists'), 1); 
		}else{
			showmessage('非法操作，请联系管理员！'); 
		}
	}
	/**
	 * 余额变动
	 */
	public function setmoney(){
		if(IS_POST){
			$user_money = (float)trim($_GET['user_money']);
			if($_GET['at']=='setDec') $user_money = -$user_money;
			$msg = trim($_GET['msg']);
			$user_id = intval($_GET['user_id']);
			if (!$user_money) showmessage('余额必须是不等于零的数字！');
			if (empty($msg)) showmessage('原因必须填写！');
			$money_info=model('user')->find($user_id);
			$data['user_id']=$user_id;
			$data['money']=$user_money;
			$data['msg']='后台余额变更;原因:'.$msg;
			$data['dateline']=NOW_TIME;
			$data['type'] = 1;
			$data['user_money'] = $money_info["user_money"]+$user_money;
			$data['freeze_money'] = $money_info["freeze_money"];
			//增加记录
			$ret = model('user_moneylog')->add($data);
			if (!$ret) showmessage('余额变更失败！');
			//改变帐户余额
			$reuslt = $this->db->where(array('id' => $user_id))->setInc('user_money',$user_money);
			if (!$reuslt) {
				model('user_moneylog')->delete($ret);
				showmessage('余额变更失败！');
			}
			/* 通知推送 */
			runhook('n_money_change',array('user_id' => $user_id,'log_id' => $ret));
			showmessage('余额变更成功', U('Member/lists'), 1);
		}else{
			showmessage('非法操作，请联系管理员！');
		}
	}
	/**
	 * 积分变动
	 */
	public function setpoints(){
		if($_POST){
			$pay_points=I('exp',0,'intval');
			if($_POST['at']=='setDec') $pay_points=-$pay_points;
			$descript=$_POST['descript'];
			$user_id=intval($_POST['user_id']);
			if ((int)$_POST['exp']==0) showmessage('积分必须是整数！');
			if (empty($descript)) showmessage('原因必须填写！');  
			$data['user_id']=$user_id;
			$points_info=model('user')->find($user_id);
			$data['descript']=$descript;
			$data['type']=1;
			$data['user_points']=$points_info["pay_points"]+$pay_points;
			$data['pay_points']=$pay_points;
			$data['time']=TIME();
			//增加记录
			$points=model('User_pointslog');
			$points->add($data);
			//改变帐户积分
			unset ($where);
			unset ($data);
			$where['id']=$user_id;
			$data['pay_points']=array('exp','pay_points + '.$pay_points);
			$this->db->where($where)->save($data);
			showmessage('积分变动成功', U('Member/lists'), 1);	 
		}else{
			showmessage('非法操作，请联系管理员！'); 
		}
	}   
	/**
	 * 经验变动
	 */
	public function setexp(){
		if($_POST){
			$expval=I('exp',0,'intval');
			$at = $_GET['at'];
			$user_id = $_GET['user_id'];
			if ($expval==0) showmessage('经验必须是整数！');
			if ($at == 'setDec') $expval = 0-$expval;
			action_exp($user_id,$expval);
			showmessage('经验变动成功', U('Member/lists'), 1);	 
		}else{
			showmessage('非法操作，请联系管理员！'); 
		}
	} 
}